#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),ts(new TreeService)
{
    ui->setupUi(this);
    ts->tw = ui->treeWidget;
    initTreeData();
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 添加目录
void MainWindow::on_action_triggered()
{
    QString dir = QFileDialog::getExistingDirectory(this,"选择文件夹","./");
    if(dir.isEmpty()){
        QMessageBox::warning(this, "警报!", "请选择一个文件夹!");
    }else{
        // 添加到根目录下
        QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget,FOLDER);
        item->setIcon(0,QIcon(":images/images/folder"));
        item->setText(0,dir);
        item->setCheckState(0,Qt::CheckState::Unchecked);
        ui->treeWidget->addTopLevelItem(item);
    }
}

// 添加文件
void MainWindow::on_action2_triggered()
{
    QTreeWidgetItem *localCurrentItem = ui->treeWidget->currentItem();
    if(localCurrentItem != nullptr)
    {
        QString path = localCurrentItem->text(0);
        QString fileName = QFileDialog::getOpenFileName(
                this,tr("open a file."),path,tr("images(*.png *.jpg *.jpeg *.bmp);;video files(*.avi *.mp4 *.wmv);;All files(*.*)"));
        if (fileName.isEmpty()) {
            QMessageBox::warning(this, "Warning!", "Failed to open the video!");
        }else{
            QTreeWidgetItem *item = new QTreeWidgetItem(localCurrentItem,FILES);
            item->setIcon(0,QIcon(":images/images/file_web"));
            item->setText(0,fileName);
            item->setCheckState(0,Qt::CheckState::Unchecked);
        }
    }
    else
    {
        QMessageBox::warning(this,"警报","选择一个文件夹节点");
    }
}

// 删除节点
void MainWindow::on_action3_triggered()
{
    QTreeWidgetItem *localCurrentItem = ui->treeWidget->currentItem();
    if(localCurrentItem != nullptr)
    {
        localCurrentItem->parent()->removeChild(localCurrentItem);
    }
    else
    {
        QMessageBox::warning(this,"警报","选择一个文件夹节点");
    }
}

// 遍历节点
void MainWindow::on_action4_triggered()
{

}

// 放大
void MainWindow::on_action5_triggered()
{

}

// 缩小
void MainWindow::on_action6_triggered()
{

}

// 实际大小
void MainWindow::on_action7_triggered()
{

}

// 适合宽度
void MainWindow::on_action8_triggered()
{

}

// 适合高度
void MainWindow::on_action9_triggered()
{

}

// 退出
void MainWindow::on_action12_triggered()
{
    this->close();
}

// 窗体浮动
void MainWindow::on_action10_toggled(bool arg1)
{
    ui->dockWidget->setFloating(arg1);
}

// 窗体是否可见
void MainWindow::on_action11_toggled(bool arg1)
{
   ui->dockWidget->setVisible(arg1);
}


// dockWidget 浮动状态变化时触发
void MainWindow::on_dockWidget_topLevelChanged(bool topLevel)
{
    ui->action10->setChecked(topLevel);
}

// dockWidget 显示状态变化时触发
void MainWindow::on_dockWidget_visibilityChanged(bool visible)
{
    ui->action11->setChecked(visible);
}

// 选中一个图片
void MainWindow::on_treeWidget_itemActivated(QTreeWidgetItem *item, int column)
{
    int localType = item->type();
    if(localType == FOLDER)
    {
        QString localText = item->text(column);
        QDir root(localText);
        QFileInfoList localEntryInfoList = root.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot);
        QList<QFileInfo>::const_iterator it = localEntryInfoList.constBegin();
        for(;it!=localEntryInfoList.constEnd();it++)
        {
            QFileInfo fileInfo = *it;
            QString localFilePath = fileInfo.filePath();
            QTreeWidgetItem *child = new QTreeWidgetItem(item,FILES);
            QIcon icon(":images/images/file_web");
            child->setIcon(0,icon);
            child->setText(0,localFilePath);
            child->setCheckState(0,Qt::CheckState::Unchecked);
        }
    }

}

/**
 * @brief MainWindow::initTreeData
 * 初始化目录数据
 */
void MainWindow::initTreeData()
{
    QString rootPath;
    rootPath.append("E:").append(QDir::separator()).append("图片");
    QDir root(rootPath);
    QFileInfoList localEntryInfoList = root.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    QList<QFileInfo>::const_iterator it = localEntryInfoList.constBegin();
    for(;it != localEntryInfoList.constEnd();it++)
    {
        QFileInfo fileInfo = *it;
        QString path = fileInfo.filePath();
        QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget,FOLDER);
        QIcon icon(":images/images/folder");
        item->setIcon(0,icon);
        item->setText(0,path);
        item->setCheckState(0,Qt::CheckState::Unchecked);
        ui->treeWidget->addTopLevelItem(item);
        ui->treeWidget->collapseItem(item);
    }
}

void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
{

}

/**
 * @brief MainWindow::on_treeWidget_currentItemChanged
 * @param current
 * @param previous
 * 当前项改变时显示图片
 */
void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    current->setCheckState(0,Qt::CheckState::Checked);
    int localType = current->type();
    if(localType == FILES)
    {
        QString localText = current->text(0);
        if(!localText.isEmpty() && localText.endsWith(".jpg"))
        {
            QImage image(localText);
            ui->label->setPixmap(QPixmap::fromImage(image));
            ui->label->adjustSize();
        }
    }

}
